﻿Imports System
Imports System.IO
Imports System.ServiceProcess



Public Class VOEISHubPushService

    Inherits System.ServiceProcess.ServiceBase 'Inherit the base service class
    Private worker As New Worker() 'Declare a Worker

    ''' <summary>
    ''' Starts the VOEIS Hub Push Service.
    ''' </summary>
    ''' <param name="args">No arguments required</param>
    ''' <remarks>This simply starts another worker thread.</remarks>
    Protected Overrides Sub OnStart(ByVal args() As String)

        Try
            'I was having problems with a full log on a test machine, so I had to add this bit of code
            Dim EL As New EventLog("Application")
            EL.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 0)

            Directory.SetCurrentDirectory(GetDir) 'Setting the current directory to the directory of the current process

            'Make sure that our event log source is registered
            If Not System.Diagnostics.EventLog.SourceExists("VOEIS Hub Push Service") Then
                System.Diagnostics.EventLog.CreateEventSource("VOEIS Hub Push Service", "Application")
            End If

            Dim WorkerThread As System.Threading.Thread 'Declare the new thread
            Dim ThreadStart As System.Threading.ThreadStart 'Declare the delegate variable
            ThreadStart = AddressOf worker.DoWork 'Assign the delegate method
            WorkerThread = New System.Threading.Thread(ThreadStart) 'Initialize the WorkerThread
            WorkerThread.Start() 'Start the thread

        Catch ex As Exception
            'Repor any errors
            System.Diagnostics.EventLog.WriteEntry("VOEIS Hub Push Service", "OnStart() exception: " & ex.Message.ToString, EventLogEntryType.Error)
        End Try

    End Sub

    ''' <summary>
    ''' Stops the VOEIS Hub Push Service
    ''' </summary>
    ''' <remarks>This method is called to stop the service.</remarks>
    Protected Overrides Sub OnStop()
        Try
            'Stop the worker thread
            worker.StopWork()
            'Initialize the mailer and send a message stating that the service has stopped
            Dim mailer As New NotifyMail()
            mailer.SendServiceMail("VOEIS Hub Push Service Terminated", "VOEIS Hub Push Service was issued a stop control at " & DateTime.Now() & " on agent " & System.Environment.MachineName.ToString())

        Catch ex As Exception
            System.Diagnostics.EventLog.WriteEntry("VOEIS Hub Push Service", "OnStop() exception: " & ex.Message.ToString, EventLogEntryType.Error)
        End Try
    End Sub

    ''' <summary>
    ''' Gets the current process's executing directory
    ''' </summary>
    ''' <returns>String</returns>
    ''' <remarks>This is used to find the app.Config.</remarks>
    Function GetDir() As String
        Dim file As FileInfo
        Dim directory As DirectoryInfo
        Dim process As System.Diagnostics.Process

        Try
            process = System.Diagnostics.Process.GetCurrentProcess
            file = New FileInfo(process.MainModule.FileName)
            directory = file.Directory
            GetDir = directory.FullName
        Finally
            file = Nothing
            directory = Nothing
            process = Nothing
        End Try
    End Function

    
End Class
